package com.test.daily.leetcode.y2021.m12.day1214;

import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2021/12/14 下午 09:43
 * @version: 1.0
 */
public class Code02_ScheduleCourse {
    public static void main(String[] args) {

    }
    public int scheduleCourse(int[][] courses){
        Arrays.sort(courses, (a,b)->{return a[1] - b[1];});
        PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2)-> { return o2 - o1;});
        int total = 0;
        for(int[] course: courses){
            int ti = course[0], di = course[1];
            if(total + ti < di){
                total += ti;
                pq.offer(ti);
            }else if(!pq.isEmpty() && total+ti <pq.peek()){
                total = total - pq.peek() + ti;
                pq.offer(ti);
            }
        }
        return pq.size();
    }
}
